﻿using System.Collections.Generic;
using CodeSaude.Domain.Interfaces.Validation;

namespace CodeSaude.Domain.Validation.Base {
    public abstract class FiscalBase<T> : IFiscal<T> where T : class {
        private readonly Dictionary<string, IRegra<T>> _validations = new Dictionary<string, IRegra<T>>();

        protected virtual void AdicionarRegra(string nomeRegra, IRegra<T> rule) {
            _validations.Add(nomeRegra, rule);
        }

        protected virtual void RemoverRegra(string nomeRegra) {
            _validations.Remove(nomeRegra);
        }

        public virtual ValidationResult Validar(T entity) {
            var result = new ValidationResult();
            foreach (var x in _validations.Keys) {
                var rule = _validations[x];
                if (!rule.Validar(entity))
                    result.AdicionarErro(rule.MensagemErro);
            }

            return result;
        }

        protected IRegra<T> ObterRegra(string nomeRegra) {
            IRegra<T> rule;
            _validations.TryGetValue(nomeRegra, out rule);
            return rule;
        }
    }
}
